package com.newblog.config;

import com.newblog.common.AppVarible;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2023-12-11
 * Time:8:59
 * Text：自定义拦截器
 */
@Configuration
public class UserInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session=request.getSession(false);
        if(session!=null && session.getAttribute(AppVarible.SESSION_USERINFO_KEY)!=null){
            //用户已登陆
            return true;
        }
        response.sendRedirect("/login.html");
        return false;
    }
}
